const mysql = require('mysql')
const DS = require('./DataStruct')
//连接数据库
connectToMysql = () => {
    // 创建连接字段
    const connection = mysql.createConnection({
        host: "81.68.160.65",   //服务器ip地址，本地服务器为localhost/127.0.0.1
        user: "node",   //用户名
        password: "B5jAbKa5y3WDyx5p",   //密码
        database: "node"    //数据库名称
    })
    connection.connect()
    return connection
}
module.exports = {
    login: (data, connect) => {
        //连接数据库，每次新的请求都要调用此函数来连接数据库
        let connection = connectToMysql()
        //利用sql语句进行查询
        connection.query('select id,name from db_user where account="' + data.account + '" and password="' + data.password + '"', (error, result) => {
            if (error) throw error
            console.log('————————————QueryResult——————————————')
            console.log(result)
            let data = new Object()
            data.code = 200
            if (result.length !== 0) {
                data.msg = "登录成功"
                data.data = result[0]
                //顺便设置当前的连接属性
                connect.id = result[0].id
                connect.name = result[0].name
                connect.isReady = false
            } else {
                data.msg = "账号或密码错误"
                data.data = null
            }
            //打包数据
            let msg = DS.packData(DS.RequestCode.USER, DS.UserAction.LOGIN, data)
            //发送消息
            connect.sendText(msg)
            // 每次查询完毕后关闭mysql连接
            connection.end((error) => {
                if (error) throw error
                console.log('login:数据库关闭成功')
            })
        })
    },
    signUp: (data, connect) => {
        if (0 < data.name.length && data.name.length <= 6
            && 6 <= data.account.length && 6 <= data.password.length) {
            //连接数据库，每次新的请求都要调用此函数来连接数据库
            let connection = connectToMysql()
            //利用sql语句进行查询该账号是否已经注册
            connection.query('select * from db_user where account="' + data.account + '"', (error, result) => {
                if (error) throw error
                //该账户未被注册
                if (result.length === 0) {
                    //利用sql语句插入数据
                    connection.query('insert into db_user(name,account,password) values("' + data.name + 
                    '","' + data.account + '","' + data.password + '")', (error, result) => {
                        if (error) throw error
                        let data = new Object()
                        if (result.length !== 0) {
                            data.code = 200
                            data.msg = "注册成功"
                            data.data = true
                        } else {
                            data.code = 200
                            data.msg = "注册失败"
                            data.data = null
                        }
                        //打包数据
                        let msg = DS.packData(DS.RequestCode.USER, DS.UserAction.SIGNUP, data)
                        //发送消息
                        connect.sendText(msg)
                        // 每次查询完毕后关闭mysql连接
                        connection.end((error) => {
                            if (error) throw error
                            console.log('signUp:数据库关闭成功')
                        })
                    })
                } else {
                    let data = new Object()
                    data.code = 200
                    data.msg = "该账号已存在"
                    data.data = null
                    //打包数据
                    let msg = DS.packData(DS.RequestCode.USER, DS.UserAction.SIGNUP, data)
                    //发送消息
                    connect.sendText(msg)
                    // 每次查询完毕后关闭mysql连接
                    connection.end((error) => {
                        if (error) throw error
                        console.log('signUp:数据库关闭成功')
                    })
                }
            })
        } else {
            let data = new Object()
            data.code = 200
            data.msg = "账号或姓名或密码格式不正确"
            data.data = null
            //打包数据
            let msg = DS.packData(DS.RequestCode.USER, DS.UserAction.SIGNUP, data)
            //发送消息
            connect.sendText(msg)
        }
    }
}
